package com.acme.bankapp.email;

import java.io.Serializable;

import com.acme.bankapp.domain.bank.ClientRegistrationListener;
import com.acme.bankapp.domain.client.Client;

/**
 * Class sends e-mail each time client added into the bank.
 */
public class EmailNotificationListener implements ClientRegistrationListener, Serializable {

    private static final String EMAIL_MESSAGE = "Glad to see you in our bank!";

    private EmailService emailService;

    /**
     * Constructor accepts an instance of the {@link EmailService}, which is
     *  used to send email message to each added client. <br>
     * Constructor does not throw an exception if the instance of the EmailService is null.
     *
     * @param emailService instance of the {@link EmailService}
     */
    public EmailNotificationListener(EmailService emailService) {
        this.emailService = emailService;
    }

    @Override
    public void onClientAdded(Client client) {
        if (emailService != null) {
            Email email = new Email();
            email.setMessage(EMAIL_MESSAGE);
            email.setClient(client);
            email.setEmailAddress(client.getEmailAddress());

            emailService.sendNotificationEmail(email);
        }
    }
}
